package com.example.administrator.myapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by admin on 2016/4/28.
 */
public class SwithButton extends View
{
    //滑动按钮的背景  滑动块的图片
    private Bitmap mBitmapBg,mBitmapBlock;
    //当前滑动按钮的状态
    private SwitchStatus mCurrStatus = SwitchStatus.CLOSE;

    /**
     * 配置到布局文件，由系统调用
     * @param context
     * @param attrs
     */
    public SwithButton(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    /**
     * 设置按钮的背景
     */
    public void setSwitchButtonBackGroundResource(int resId)
    {
        mBitmapBg = BitmapFactory.decodeResource(getResources(),resId);
    }

    /**
     * 设置滑动块的图片
     */
    public void setSwitchButtonBlockResource(int resId)
    {
        mBitmapBlock = BitmapFactory.decodeResource(getResources(),resId);
    }

    /**
     * 设置滑动按钮的开关状态
     */
    public void setSwitchButtonStatus(SwitchStatus status)
    {
        mCurrStatus = status;
    }

    public enum SwitchStatus
    {
            OPEN,
            CLOSE
    };

    @Override
    protected void onDraw(Canvas canvas)
    {
        canvas.drawBitmap(mBitmapBg,0,0,null);

        //如果是滑动的状态
        if(mIsSlinding)
        {
            float left = mX - mBitmapBlock.getWidth()/2;
            if(left<0)
            {
                left = 0;
            }
            if(left>mBitmapBg.getWidth()-mBitmapBlock.getWidth())
            {
                left = mBitmapBg.getWidth()-mBitmapBlock.getWidth();
            }
            canvas.drawBitmap(mBitmapBlock,left,0,null);
        }
        else
        {
                //根据当前按钮的状态来绘制
            if(mCurrStatus==SwitchStatus.OPEN)
            {
                canvas.drawBitmap(mBitmapBlock,mBitmapBg.getWidth()-mBitmapBlock.getWidth(),0,null);
            }
            else
            {
                canvas.drawBitmap(mBitmapBlock,0,0,null);
            }
        }

        super.onDraw(canvas);
    }

    float mX;
    boolean mIsSlinding = false;
    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        mX=event.getX();
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                mIsSlinding = true;
                break;
            case MotionEvent.ACTION_MOVE:
                mIsSlinding = true;
                break;
            case MotionEvent.ACTION_UP:
                mIsSlinding = false;
                //如果手指松开了，判断当前是打开还是关闭
                int centerX = mBitmapBg.getWidth()/2;
                if(mX<centerX)
                {
                    if(mCurrStatus==SwitchStatus.OPEN)
                    {
                        mCurrStatus = SwitchStatus.CLOSE;
                        if(onSwitchStatusChangeListener!=null)
                        {
                            onSwitchStatusChangeListener.onChange(mCurrStatus);
                        }
                    }
                }
                else
                {
                    if(mCurrStatus==SwitchStatus.CLOSE)
                    {
                        mCurrStatus = SwitchStatus.OPEN;
                        if(onSwitchStatusChangeListener!=null)
                        {
                            onSwitchStatusChangeListener.onChange(mCurrStatus);
                        }
                    }
                }
                break;
        }
        //使控件失效
        invalidate();
        //返回true，表示自己将事件进行消化，不需要再往下传递
        return true;
    }

    private OnSwitchStatusChangeListener onSwitchStatusChangeListener;
    //用户设置事件
    public void setOnSwitchStatusChangeListener(OnSwitchStatusChangeListener onSwitchStatusChangeListener)
    {
        this.onSwitchStatusChangeListener = onSwitchStatusChangeListener;
    }
    //回调事件
    public interface OnSwitchStatusChangeListener
    {
        void onChange(SwitchStatus status);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        //滑动按钮，背景有多大，它就有多大
        setMeasuredDimension(mBitmapBg.getWidth(),mBitmapBg.getHeight());
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
